package com.qs.home.ui.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.databinding.Observable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.qs.base.base.BaseMyFragment;
import com.qs.base.router.RouterFragmentPath;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeFragmentOriginalBinding;
import com.qs.home.entity.TextResultEntity;
import com.qs.home.ui.VM.IdentifyDetailsViewModel;
import com.qs.home.ui.activity.IdentifyDetailsActivity;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 识别结果详情文字
 *
 * @Author ltzz
 * @Date 2019/8/20
 */
@Route(path = RouterFragmentPath.Home.TEXT_FRAGMENT)
public class TextFragment extends BaseMyFragment<HomeFragmentOriginalBinding, IdentifyDetailsViewModel> {
    //识别结果数据
    @Autowired(name = "textResult")
    TextResultEntity textResult;

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    protected boolean statusBarDarkFont() {
        return false;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.home_fragment_text;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (textResult != null) {
            viewModel.textResultOb.set(textResult);
            if (textResult.getWords_result() != null && textResult.getWords_result().size() > 0) {
                String result = "";
                for (int i = 0; i < textResult.getWords_result().size(); i++) {
                    if (i == 0)
                        result = textResult.getWords_result().get(i).getWords();
                    else
                        result = result + textResult.getWords_result().get(i).getWords();
                }
                viewModel.textResultStr.set(result);
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //复制
        viewModel.uc.copyTextUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", viewModel.textResultStr.get());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtils.showShort("文本已复制");
            }
        });
        //翻译
        viewModel.uc.translateUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                IdentifyDetailsActivity activity = (IdentifyDetailsActivity) getActivity();
                activity.pageChangedTranslate(viewModel.textResultStr.get());
            }
        });
    }
}
